using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ID3;
namespace JamD3
{
    public partial class llCopySelectedTextToTb : Form
    {
        public llCopySelectedTextToTb()
        {
            InitializeComponent();

        }

        ConcertText ctFile;
        String delimiter = "\r\n";

        private void btnStartWizard_Click(object sender, EventArgs e)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {
            ctFile.AlbumTitle = tbSelection.Text;
        }

        private void btnCopyToSingleLineTB_Click(object sender, EventArgs e)
        {

        }

        private void llOpenInformationFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ctFile = new ConcertText();
                    ctFile.LoadConcertTextFile(ofd.FileName);
                    tbTextFile.Text = ctFile.FileContents;
                }

                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message + "\r\n\r\nPlease Try Opening File Again.", "Error Opening Selected File");
                    return;
                }

                tcMain.Enabled = true;
            }
            //TODO: Figure out why I did this.
            /*else
            {
                if (ctFile.FileContents.Equals("") || ctFile.TextFilePath.Equals(""))
                {
                    tcMain.Enabled = false;
                }
            }*/
        }



        private void llCopyToProperty_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            String newText = tbSelection.Text.Trim();
            if (rbArtist.Checked)
            {
                ctFile.AlbumArtist = newText;
            }
            else if (rbTitle.Checked)
            {
                ctFile.AlbumTitle = newText;
            }
            else if (rbYear.Checked)
            {
                //Perform Validation
                if (IsValidYear(newText))
                {
                    ctFile.AlbumYear = newText;
                }

                else
                {
                    MessageBox.Show("Entry is not a valid year.\r\nPlease review the entry and try again.", "Error");
                }
            }
            else //default case
            {
                MessageBox.Show("Please select a destination radio button before continuing.", "Error");
                return;
            }
            RefreshLabels();
            return;
        }

        private void RefreshLabels()
        {
            lblTitle.Text = ctFile.AlbumTitle;
            lblYear.Text = ctFile.AlbumYear;
            lblArtist.Text = ctFile.AlbumArtist;
        }

        private bool IsValidYear(string p)
        {
            try
            { Convert.ToInt32(p); }
            catch
            {
                return false;
            }
            return true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            tbSelection.Text = tbTextFile.SelectedText.Replace("\r\n", " - ");
        }

        private void llAppendTextToTb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            tbSelection.Text += tbTextFile.SelectedText.Replace("\r\n", " - ").Trim();
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Delimiter del = new Delimiter();
            del.DelimiterString = delimiter;
            if (del.ShowDialog() == DialogResult.OK)
            {
                delimiter = del.DelimiterString;
                lblDelimiter.Text = ("Delimiter: " + delimiter);
            }

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            String[] delimiters = { delimiter };
            String[] ListOfTracks = tbTextFile.SelectedText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);


            foreach (String s in ListOfTracks)
            {
                lbTrackText.Items.Add(s.Trim());
            }


        }

        private void llChooseMp3Files_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Filter = "mp3 Audio Files (*.mp3)|*.mp3";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (string strFile in ofd.FileNames)
                {
                    lbFileText.Items.Add(strFile);
                }
            }
            else
            {
            }
        }

        private void lbTrackText_Click(object sender, EventArgs e)
        {
            /*EditSongTitle est = new EditSongTitle();
            est.SongTitle = (String)lbTrackText.SelectedItem;
            if (est.ShowDialog() == DialogResult.OK)
            {
                lbTrackText.Items[lbTrackText.SelectedIndex] = est.SongTitle;
            }*/
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < lbFileText.Items.Count; i++)
            {

                string strFile = lbFileText.Items[i].ToString();
                ID3Info i3 = new ID3Info(strFile, true);
                i3.ID3v1Info.Album = ctFile.AlbumTitle;
                i3.ID3v1Info.Artist = ctFile.AlbumArtist;
                i3.ID3v1Info.Year = ctFile.AlbumYear;
                i3.ID3v1Info.Title = lbTrackText.Items[i].ToString();
                i3.Save();
            }
        }

        /// <summary>
        /// Event handler for the context menu item with text property "Move Up"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miMoveUp_Click(object sender, EventArgs e)
        {
            ListBox lbSender = (ListBox)menuSongList.SourceControl;
            //if there is no 'up' from the selected item...
            if (lbSender.SelectedIndex == 0)
                return;
            //else if(everything's ok)...
            int from = lbSender.SelectedIndex;
            switchTrackPositions(lbSender, from, from - 1);
            return;
        }

        /// <summary>
        /// Handles the menu item "Move Down" onClick() event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miMoveDown_Click(object sender, EventArgs e)
        {
            int from;
            ListBox lbSender = (ListBox)menuSongList.SourceControl;
            //if there is no 'down' position to go to...
            if ((from = lbSender.SelectedIndex) == lbSender.Items.Count - 1)
            {
                return;
            }

            //else if (everything's alright)...
            switchTrackPositions(lbSender, from, from + 1);
        }

        /// <summary>
        /// Handles onClick() event for context menu item "Delete Item"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miDelete_Click(object sender, EventArgs e)
        {
            ListBox lbSender = (ListBox)menuSongList.SourceControl;
            DeleteTrackListing(lbSender,
                lbTrackText.SelectedIndex);
        }


        /// <summary>
        /// Switches track positions to facilitate the 'move' operations
        /// </summary>
        /// <param name="sender">ListBox on which to perform the operation.</param>
        /// <param name="from">Integer index that represents the original location</param>
        /// <param name="to">Integer indes that represents the new location</param>
        private void switchTrackPositions(ListBox sender, int from, int to)
        {
            //sanity check
            if (from > sender.Items.Count - 1 ||
                from < 0 ||
                to > sender.Items.Count - 1 ||
                to < 0)
            { return; }

            object oFrom = sender.Items[from];
            object oTo = sender.Items[to];

            sender.Items[from] = oTo;
            sender.Items[to] = oFrom;

            return;
        }



        private void DeleteTrackListing(ListBox sender, int from, int to)
        {
            //sanity check...
            #region ----------------- VALIDATION --------------------------
            if (to < from || to > sender.Items.Count - 1 || to < 0 || from > sender.Items.Count - 1 || from < 0)
            {
                return;
            }
            #endregion

            int pointer = from;
            while (pointer <= to)
            {
                //sanity check
                if (pointer >= 0 && pointer < sender.Items.Count)
                {
                    sender.Items.RemoveAt(pointer);
                }
                else
                {
                    return;
                }
                pointer++;
            }
            //successfully made it through the fxn
            return;
        }


        /// <summary>
        /// Proxy function which deletes one item in the listbox
        /// </summary>
        /// <param name="lbSender">the ListBox from which the call originated</param>
        /// <param name="p">Index of the item to delete.</param>
        private void DeleteTrackListing(ListBox lbSender, int p)
        {
            DeleteTrackListing(lbSender, p, p);
        }

        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteTrackListing((ListBox)menuSongList.SourceControl, 0, ((ListBox)menuSongList.SourceControl).Items.Count - 1);
        }



        private void lbTrackText_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListBox lb = ((ListBox)menuSongList.SourceControl);
                int ndex = lb.IndexFromPoint(new Point(e.X, e.Y));
                if (ndex < 0 || ndex > lb.Items.Count - 1)
                    return;
                lb.SelectedIndex = ndex;
            }
        }

        private void lbTrackText_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                base.OnMouseClick(new MouseEventArgs(MouseButtons.Left,
                    e.Clicks, e.X, e.Y, e.Delta));
            }

        }


    }
}